package com.zhuiyun.project.api.check.service.serviceImpl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.resource.MultiResource;
import cn.hutool.core.util.ObjectUtil;
import com.zhuiyun.project.api.check.entity.Check;
import com.zhuiyun.project.api.check.mapper.CheckMapper;
import com.zhuiyun.project.api.check.model.CheckModel;
import com.zhuiyun.project.api.check.service.CheckService;
import com.zhuiyun.project.api.message.entity.Message;
import com.zhuiyun.project.api.message.service.MessageService;
import com.zhuiyun.project.api.messagelist.entity.MessageList;
import com.zhuiyun.project.api.messagelist.service.MessageListService;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.util.BeanUtils;
import com.zhuiyun.project.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.SimpleFormatter;

@Service
public class CheckServiceImpl implements CheckService {

    @Autowired
    CheckMapper checkMapper;
    @Autowired
    private MessageService messageService;

    @Autowired
    private MessageListService messageListService;

    /**
     * @param checkModel
     * @Author kdj
     * @Description 添加系统消息
     * @Date 2023/8/6 1:43
     * @Return java.lang.Integer
     */
    @Override
    public Integer insertCheck(CheckModel checkModel) {

        if (ObjectUtil.isNotEmpty(checkModel)) {
            Check check = new Check();
            BeanUtils.copy(checkModel, check);
            Example updatListen = new Example(Check.class);
            updatListen.createCriteria().andEqualTo("isLast", 1)
                    .andEqualTo("delFlag", 0);
            Example.Criteria orOne = updatListen.createCriteria().andEqualTo("toUserId", check.getToUserId())
                    .andEqualTo("fromUserId", check.getFromUserId());
            updatListen.and(orOne);
            Example.Criteria orTwo = updatListen.createCriteria().andEqualTo("toUserId", check.getFromUserId())
                    .andEqualTo("fromUserId", check.getToUserId());
            updatListen.or(orTwo);
            Check check1 = new Check();
            check1.setIsLast(0);

            checkMapper.updateByExampleSelective(check1, updatListen);
            //添加到message表中  类型：系统消息
            int messageId = messageService.insertMsg(new Message(checkModel.getUuid(), 3));
            check.setMessageId(messageId);

            check.setDelFlag(0);
            //最后聊天信息
            check.setIsLast(1);
            check.setIsRead(0);
            check.setIsDelete(0);
            check.setUpdateTime(new Date());
            check.setCheckTime(new Date());
            check.setCreateTime(new Date());
            checkMapper.insertSelective(check);
            //增加或者修改messageList
            messageListService.updateChatList(messageId);
            return check.getId();
        }

        return 0;
    }


    /**
     * @param toUserId
     * @Author kdj
     * @Description 获取审核消息列表
     * @Date 2023/8/6 1:49
     * @Return com.zhuiyun.project.common.response.CommonResult
     */
    @Override
    public CommonResult getSystemList(int toUserId) {
        List<CheckModel> checks = checkMapper.getSystemList(toUserId);
        //转换成map
        List<Map> list = new ArrayList<>(checks.size());
        for (CheckModel check : checks) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            String time = simpleDateFormat.format(check.getCreateTime());
            Map<String, Object> map = BeanUtil.beanToMap(check, false, true);
            map.put("createTime",time);
            list.add(map);
        }
        return CommonResult.ok(list);
    }

    /**
     * @Author 小江
     * @Description //TODO 根据id获取用户的消息
     * @Date 19:36 2023/7/23
     **/
    @Override
    public CommonResult getUserById(Integer toUserId) {
        Check check = checkMapper.selectByPrimaryKey(toUserId);

        return CommonResult.ok(check);
    }


    /**
     * @Author 小江
     * @Description //TODO 查询系统消息
     * @Date 9:53 2023/7/24
     **/
    @Override
    public CommonResult getCheckRecord(int toId) {
        return null;
    }


    /**
     * @param checkModel
     * @Author kdj
     * @Description 修改未读状态
     * @Date 2023/8/6 1:53
     * @Return int
     */
    @Override
    public int maskDel(CheckModel checkModel) {
        String uuid = checkModel.getUuid();
        Integer toUserId = checkModel.getToUserId();
        int i = checkMapper.maskDel(uuid, toUserId);
        return i;
    }
}
